package com.lanhao.aot.carrierintegration.track17;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.http.HttpRequest;

public class Track17IntegrationUtil {
	private static final Logger LOG = LoggerFactory.getLogger(Track17IntegrationUtil.class);
	public static final String BASE_URL = "https://api.17track.net/track";
	public static final String REG_URL = BASE_URL + "/v2/register";
	public static final String CHG_URL = BASE_URL + "/v2/changeinfo";
	public static final String SECRET_KEY = "5AC4AB5BF0B50424DE5FBB8C6767A854";
	public static final Map<String, String> Carrier2TrackMap = new HashMap<>();
	
	public interface Carrier {
		public static final String CANADA_POST = "Canada Post"; // 加拿大邮政
		public static final String AUSTRALIA_POST = "Australia Post"; // 澳大利亚邮政
		public static final String ROYAL_MAIL = "Royal Mail"; // 英国皇家邮政
		public static final String USPS = "USPS";
		public static final String UPS = "UPS";
		public static final String GLS = "GLS";
		public static final String CORREOS = "CORREOS";
		public static final String FEDEX = "FEDEX"; // 联邦快递
		public static final String COLISSIMO = "Colissimo"; // 法国邮政
		public static final String DHL = "DHL";
		public static final String DHL_PAKET = "DHL_Paket";	// DHL_PAKET
		public static final String DHL_PARCEL = "DHL_Parcel"; // DHL_PARCEL
		public static final String NACEX = "NACEX";
		public static final String CTT_EXPRESS = "CTT_Express";
		
		public static final String IN_POST  = "InPost"; // 波兰InPost
		public static final String RUSSIAN_POST  = "Russian_Post"; // 俄罗斯邮政
		public static final String POSTE_ITALIANE  = "Posteitaliane"; // 意大利邮政
		public static final String NAQEL = "Naqel";
		public static final String CDEK = "CDEK";
		public static final String BPOST  = "Bpost"; // 比利时邮政
		public static final String CORREIOS  = "Correios"; // 巴西邮政
		public static final String CJ_Korea_Express = "CJ Korea Express"; // CJ大韩通运
		public static final String POSTNL = "PostNL"; // 荷兰邮政
		public static final String ESTAFETA = "Estafeta"; // 墨西哥Estafeta
		public static final String JITU_SA = "JTExpress(SA)"; // 极兔物流（SA）
		public static final String JITU_AE = "JTExpress(AE)"; // 极兔物流（AE）
		
		public static final String UBI = "UBI";
		public static final String ESPOST = "ESPOST";
		public static final String ESPACE = "ESPACE";
	}
	
	static {
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.CANADA_POST, "3041");
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.AUSTRALIA_POST, "1151");
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.ROYAL_MAIL, "11031");
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.USPS, "21051");
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.UPS, "100002");
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.GLS, "100005");
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.CORREOS, "19181");
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.FEDEX, "100003");
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.COLISSIMO, "6051");
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.DHL_PAKET, "7041");
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.NACEX, "100436"); // 葡萄牙Nacex
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.CTT_EXPRESS, "100114"); // 葡萄牙CTT Express
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.IN_POST, "100043"); // 波兰InPost
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.RUSSIAN_POST, "18031"); // 俄罗斯邮政
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.POSTE_ITALIANE, "9071"); // 意大利邮政
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.CDEK, "100030"); // 俄罗斯CDEK
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.BPOST, "2061"); // 比利时邮政  2063 ? 国际版本
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.CJ_Korea_Express, "100116"); // 韩国大通
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.ESTAFETA, "100139"); // 墨西哥ESTAFETA
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.CORREIOS, "2151"); // 巴西邮政
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.JITU_SA, "100402"); // 极兔SA
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.JITU_AE, "100856"); // 极兔AE
		Carrier2TrackMap.put(Track17IntegrationUtil.Carrier.POSTNL, "14041"); // 荷兰邮政
	}
	
	public static void main(String[] args) {
		changeInfo("3SJESF803773382", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF032334826", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF178346239", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF134418585", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF613466510", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF571312696", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF283266109", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF267135800", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF446874994", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF066651956", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF215797620", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF892427790", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF641709501", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF712887408", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF586286255", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF842158614", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF227329202", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF211389896", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF815318453", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF550899365", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF264445209", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
//		changeInfo("3SJESF282541483", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
//		changeInfo("3SJESF684304799", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
//		changeInfo("3SJESF005551469", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
//		changeInfo("3SJESF775670203", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF768545670", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF363162987", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF426486668", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF261480075", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF974944739", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF266199561", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF839285452", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF689023057", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF569601797", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF632229305", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF746920654", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF345903908", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF429361125", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF498824402", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF566590881", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF297481123", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF471633510", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF143533070", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF408203367", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF722687023", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF156499648", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF780816456", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
		changeInfo("3SJESF234973321", Track17IntegrationUtil.Carrier.POSTNL, "6460WB");
	}
	
	
	public static String trackNumberRegister(String trackNumber, String carrier, String postCode) {
		LOG.info("向17track注册物流单信息trackNumber={}，carrier={}", trackNumber, carrier);
		String carrierCode = Carrier2TrackMap.get(carrier);
		if (StringUtils.isBlank(carrierCode)) {
			return "Fail:暂不支持该物流商运单号注册功能";
		}
		JSONArray request = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("number", trackNumber);
		item.put("carrier", carrierCode);
		item.put("param", postCode); // 收件地址的邮编
		request.add(item);
		String requestBody = request.toString();
		LOG.info("调用17Track运单注册接口开始，url={}, requestBody={}", REG_URL, requestBody);
		String registerResult = HttpRequest.post(REG_URL).header("17token", SECRET_KEY).header("Content-Type", "application/json")
				.setConnectionTimeout(1000).setReadTimeout(10000).body(requestBody).execute().body();
		LOG.info("调用17Track运单注册接口结束，respBody={}", registerResult);
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return registerResult;
	}
	
	public static String changeInfo(String trackNumber, String carrier, String postCode) {
		LOG.info("向17track修改物流单信息trackNumber={}，carrier={}", trackNumber, carrier);
		String carrierCode = Carrier2TrackMap.get(carrier);
		if (StringUtils.isBlank(carrierCode)) {
			return "Fail:暂不支持该物流商运单号修改功能";
		}
		JSONArray request = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("number", trackNumber);
		item.put("carrier", carrierCode);
		JSONObject items = new JSONObject();
		items.put("param", "NL-" + postCode);
		item.put("items", items);
		request.add(item);
		String requestBody = request.toString();
		LOG.info("调用17Track运单修改接口开始，url={}, requestBody={}", CHG_URL, requestBody);
		String registerResult = HttpRequest.post(CHG_URL).header("17token", SECRET_KEY).header("Content-Type", "application/json")
				.setConnectionTimeout(1000).setReadTimeout(10000).body(requestBody).execute().body();
		LOG.info("调用17Track运单修改接口结束，respBody={}", registerResult);
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return registerResult;
	}
}
